import { defineStore } from "pinia";
import axios from "axios";
import { nanoid } from "nanoid";
//store选项式写法--第二个参数，对象形式
// export const useLoveTalkStore = defineStore("talk", {
//   state: () => {
//     return {
//       talkList: localStorage.getItem("talkList")
//         ? JSON.parse(localStorage.getItem("talkList") as string)
//         : [],
//     };
//   },
//   actions: {
//     async getATalk() {
//       let {
//         data: { content: title },
//       } = await axios.get("/api/api/rand.qinghua?format=json");
//       console.log("title", title);
//       let obj = {
//         id: nanoid(),
//         title, //可以直接简写 title
//       };
//       this.talkList.unshift(obj);
//     },
//   },
// });
//store组合式写法--第二个参数，函数形式
import { reactive } from "vue";
export const useLoveTalkStore = defineStore("talk", () => {
  //talkList就是state数据
  const talkList = reactive(
    JSON.parse(localStorage.getItem("talkList") as string) || []
  );

  //getATalk函数就是actions方法
  async function getATalk() {
    let {
      data: { content: title },
    } = await axios.get("/api/api/rand.qinghua?format=json");
    console.log("title", title);
    let obj = {
      id: nanoid(),
      title, //可以直接简写 title
    };
    talkList.unshift(obj);
  }
  return { talkList, getATalk }; //最后return出去
});
